﻿using System;
using System.Collections.Generic;
using System.Text;
using Synthetic.MicroApps.Framework;

namespace Synthetic.MicroApps.Loader
{
    class Program
    {
        private static Dictionary<int, string> Options = new Dictionary<int, string>();
        private static int OptionNumber = 1;
        private static IMAFramework[] plugins;

        static void Main(string[] args)
        {
            Console.Title = "Synthetic.MicroApps.Loader";
            Console.WriteLine("MicroApp Loader by Connor Harries");
            plugins = Loader.GetPlugins(AppDomain.CurrentDomain.BaseDirectory + "\\plugins");
            if (plugins.Length == 0)
            {
                Environment.Exit(1);
            }
            else
            {
                for (int i = 0; i < plugins.Length; i++)
                {
                    Options.Add(i, plugins[i].Name);
                }
                if (plugins.Length == 1)
                {
                    Console.WriteLine("1 MicroApp found!");
                }
                else
                {
                    Console.WriteLine("{0} MicroApps found!", plugins.Length);
                }
                Console.WriteLine();
                SelectOption();
            }
        }

        private static void SelectOption()
        {
            foreach (string o in Options.Values)
            {
                Console.WriteLine("\t" + OptionNumber + ". " + o.ToString());
                OptionNumber += 1;
            }
            Console.WriteLine();
            Console.Write("Enter number of MicroApp you wish to load: ");
            int opts;
            string x = Console.ReadLine();
            bool parsed;
            parsed = int.TryParse(x, out opts);
            ExecuteOption(opts);
        }

        private static void ExecuteOption(int PluginNumber)
        {
            plugins = Loader.GetPlugins(AppDomain.CurrentDomain.BaseDirectory + "\\plugins");
            plugins[PluginNumber - 1].Action();
        }
    }
}
